package com.bruce.spring.http.client.controller;

import com.bruce.spring.http.api.OrderClient;
import com.bruce.spring.http.api.Page;
import com.bruce.spring.http.api.Store;
import com.bruce.spring.http.client.factory.ClientFactory;
import com.bruce.spring.http.client.util.JsonUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Map;
import java.util.Set;

/**
 * Created by bruce in 2025/6/17 02:19
 */
@RestController
@RequestMapping("/order")
public class OrderClientController {
    private static final Logger log = LoggerFactory.getLogger(OrderClientController.class);

    private final ClientFactory clientFactory;

    private RestTemplate restTemplate;

    public OrderClientController(ClientFactory clientFactory, HttpMessageConverters converters) {
        this.clientFactory = clientFactory;
        this.restTemplate = new RestTemplate(converters.getConverters());
    }


    @PutMapping(value = "/putJsonStores")
    public Page<Store> postJsonStores(Store store) {
        log.info("postJsonStores: store:{}", store);
        OrderClient orderClient1 = clientFactory.getClient("D1", OrderClient.class);
        Page<Store> page = orderClient1.putJsonStores(store);
        log.info("postJsonStores: {}", JsonUtil.toJson(page));
        return page;
    }

    @PutMapping(value = "/putFromStores")
    public Page<Store> postFromStores(Store store) {
        OrderClient orderClient1 = clientFactory.getClient("D1", OrderClient.class);
        Page<Store> page = orderClient1.putFormStores(store);
        log.info("postFromStores: page:{}", page);
        return page;
    }

    // Springmvc中可以使用Map<String, Object>接收所有的请求参数, 但是必现要加@RequestBody或者@RequestParam注解
    @PutMapping(path = "/requestMap")
    public Object requestMap(@RequestParam Map<String, String> params, HttpServletRequest request) {
        log.info("request url:{}", request.getRequestURI());
        log.info("request.getQueryString():{}", request.getQueryString());
        log.info("request.getParameterMap():{}", JsonUtil.toJson(request.getParameterMap()));
        log.info("requestMap: params:{}", params);

        OrderClient orderClient1 = clientFactory.getClient("D1", OrderClient.class);
        Store store = orderClient1.requestMap(params);
        log.info("requestMap: store:{}", JsonUtil.toJson(store));
        return store;
    }

    @PutMapping(path = "/requestMapBody")
    public Object requestMapBody(@RequestBody Map<String, String> params, HttpServletRequest request) {
        OrderClient orderClient1 = clientFactory.getClient("D1", OrderClient.class);
        Map<String, String> result = orderClient1.requestMapBody(params);
        log.info("requestMapBody result:{}", JsonUtil.toJson(result));
        return result;
    }

    @PutMapping(path = "/responseEntity", consumes = "application/x-www-form-urlencoded")
    public Object responseEntity(@RequestParam Map<String, String> params) {
        OrderClient orderClient1 = clientFactory.getClient("D1", OrderClient.class);
        ResponseEntity<Store> storeResponseEntity = orderClient1.responseEntity(params);
        HttpHeaders headers = storeResponseEntity.getHeaders();

        ResponseEntity<Store> storeResponseEntity1 = new ResponseEntity<>(storeResponseEntity.getBody(), storeResponseEntity.getStatusCode());

        return storeResponseEntity1;
    }


    @PutMapping(path = "/responseEntity2", consumes = "application/x-www-form-urlencoded")
    public Object responseEntity2(@RequestParam Map<String, String> params) {

        log.info("responseEntity2: params:{}", params);

        URI uri = URI.create("http://localhost:8080/order/responseEntity");

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/x-www-form-urlencoded");
        // httpHeaders.add("Connection", "close");

        LinkedMultiValueMap<String, String> requestParam = new LinkedMultiValueMap<>();
        params.forEach(requestParam::add);

        RequestEntity<LinkedMultiValueMap<String, String>> requestEntity = new RequestEntity<>(requestParam, httpHeaders, HttpMethod.PUT, uri);
        ResponseEntity<Store> responseEntity = restTemplate.exchange(requestEntity, Store.class);

        ResponseEntity<Store> storeResponseEntity1 = new ResponseEntity<>(responseEntity.getBody(), responseEntity.getStatusCode());

        return storeResponseEntity1;
    }


    @PutMapping(value = "/stores/{storeId}", consumes = "application/json", params = "mode=upsert")
    public Store usePathVars(@PathVariable("storeId") Long storeId, @RequestBody Store store) {
        Store store1 = clientFactory.getClient("D1", OrderClient.class).usePathVars(storeId, store);
        log.info("usePathVars response:{}", store1);
        return store1;
    }


    @GetMapping(value = "/keySet/{storeId}")
    public Set<String> keySet(@PathVariable("storeId") Long storeId) {
        OrderClient orderClient = clientFactory.getClient("D1", OrderClient.class);
        Set<String> strings = orderClient.keySet(1L, null);
        log.info("keySet: {}", JsonUtil.toJson(strings));
        return strings;
    }


}
